import random

from django.core.management.base import BaseCommand
from django.db import transaction

from map.models import *

class Command(BaseCommand):
    help = "generates new map elements"

    @transaction.commit_on_success
    def handle(self, *args, **options):
        cells = list(Cell.objects.all().order_by('y', 'x'))
        for cell in cells:
            if cell.content.
            for y in xrange(0, Map.size_y):
                content = CellContent.get_random_content()
                content.save()
                if isinstance(content, Settlement):
                    for n in xrange(0,20):
                        GroundSlot.objects.create(
                            settlement=content, 
                            position = n,
                            ground_type = random.choice(
                                [GroundSlot.TYPE.CORN,
                                 GroundSlot.TYPE.IRON,
                                 GroundSlot.TYPE.CLAY,
                                 GroundSlot.TYPE.WOOD]))
                    for n in xrange(0,10):
                        BuildingSlot.objects.create(
                            settlement=content, 
                            position = n,
                            building_type = BuildingSlot.TYPE.EMPTY)
                cell = Cell.objects.create(x = x, y = y, content = content)
                cell.save()

